```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Netty非阻塞I/O模型解析 | 技术小馆</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.8;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .code-block {
            background-color: #2d2d2d;
            color: #f8f8f2;
            border-radius: 8px;
            position: relative;
        }
        .code-block::before {
            content: attr(data-lang);
            position: absolute;
            top: 10px;
            right: 15px;
            color: rgba(255,255,255,0.5);
            font-size: 0.8rem;
        }
        .concept-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .highlight-box {
            position: relative;
            border-left: 4px solid #6e8efb;
        }
        .highlight-box::before {
            content: "重点";
            position: absolute;
            top: -10px;
            left: -15px;
            background: #6e8efb;
            color: white;
            padding: 2px 10px;
            border-radius: 20px;
            font-size: 0.8rem;
            font-weight: bold;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col items-center text-center">
                <span class="bg-white bg-opacity-20 px-4 py-1 rounded-full text-sm mb-4">高性能网络编程</span>
                <h1 class="text-4xl md:text-5xl font-bold mb-6">Netty的非阻塞I/O模型</h1>
                <p class="text-xl md:text-2xl opacity-90 max-w-3xl leading-relaxed">
                    探索现代网络应用中高性能通信的核心机制
                </p>
                <div class="mt-10 flex space-x-4">
                    <a href="#content" class="bg-white text-purple-700 px-6 py-3 rounded-full font-medium hover:bg-opacity-90 transition duration-300">
                        <i class="fas fa-book-open mr-2"></i>开始阅读
                    </a>
                    <a href="#diagram" class="border-2 border-white border-opacity-50 text-white px-6 py-3 rounded-full font-medium hover:bg-white hover:bg-opacity-10 transition duration-300">
                        <i class="fas fa-project-diagram mr-2"></i>查看图表
                    </a>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main id="content" class="container mx-auto max-w-5xl px-4 md:px-0 py-16">
        <!-- Section 1 -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <span class="text-5xl font-bold text-purple-600 opacity-20 mr-4">01</span>
                <h2 class="text-3xl font-bold">非阻塞 I/O 模型概述</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="prose max-w-none">
                    <p class="text-lg">非阻塞 I/O 模型指的是在进行 I/O 操作时，线程不会被阻塞，线程可以在等待 I/O 操作完成时执行其他任务。</p>
                    <p>在传统的阻塞 I/O 模型中，线程发起 I/O 请求后会被阻塞，直到 I/O 操作完成。在非阻塞 I/O 模型中，线程发起 I/O 请求后，如果操作无法立即完成，线程会返回并继续执行其他任务，直到 I/O 操作可以完成时再处理结果。</p>
                    
                    <div class="highlight-box p-6 bg-gray-50 rounded-lg mt-6">
                        <p class="font-medium">Netty 通过使用 Java NIO（New I/O）中的 <code>Selector</code> 和 <code>Channel</code> 来实现非阻塞 I/O 模型。</p>
                    </div>
                </div>
                
                <div class="bg-gray-100 rounded-xl p-6 flex items-center justify-center">
                    <div class="w-full">
                        <div class="mermaid">
                            graph LR
                                A[客户端请求] --> B[Selector]
                                B --> C[Channel 1]
                                B --> D[Channel 2]
                                B --> E[Channel 3]
                                style A fill:#6e8efb,color:white
                                style B fill:#a777e3,color:white
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 2 -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <span class="text-5xl font-bold text-purple-600 opacity-20 mr-4">02</span>
                <h2 class="text-3xl font-bold">Netty 的非阻塞 I/O 模型架构</h2>
            </div>
            
            <div class="prose max-w-none mb-8">
                <p>Netty 采用了基于事件驱动的异步 I/O 模型，通过事件循环（EventLoop）来管理 I/O 事件。核心组件包括：</p>
                
                <div class="grid md:grid-cols-3 gap-6 mt-8">
                    <div class="concept-card bg-white p-6 rounded-xl shadow-md transition duration-300">
                        <div class="text-purple-600 mb-4">
                            <i class="fas fa-plug text-3xl"></i>
                        </div>
                        <h3 class="font-bold text-xl mb-2">Channel</h3>
                        <p class="text-gray-600">表示与网络通信的端点</p>
                    </div>
                    
                    <div class="concept-card bg-white p-6 rounded-xl shadow-md transition duration-300">
                        <div class="text-purple-600 mb-4">
                            <i class="fas fa-bell text-3xl"></i>
                        </div>
                        <h3 class="font-bold text-xl mb-2">Selector</h3>
                        <p class="text-gray-600">用于监听多个通道的 I/O 事件</p>
                    </div>
                    
                    <div class="concept-card bg-white p-6 rounded-xl shadow-md transition duration-300">
                        <div class="text-purple-600 mb-4">
                            <i class="fas fa-sync-alt text-3xl"></i>
                        </div>
                        <h3 class="font-bold text-xl mb-2">EventLoop</h3>
                        <p class="text-gray-600">负责在非阻塞的情况下处理 I/O 事件</p>
                    </div>
                </div>
            </div>
            
            <div class="mt-10">
                <h3 class="text-2xl font-bold mb-4">代码实现示例</h3>
                <div class="code-block p-6 overflow-x-auto" data-lang="Java">
                    <pre><code class="language-java">public class NioEventLoop extends SingleThreadEventLoop {
    private final Selector selector;
    
    public NioEventLoop(EventLoopGroup parent, ThreadFactory threadFactory) {
        super(parent, threadFactory);
        this.selector = Selector.open(); // 创建 Selector 实例
    }

    @Override
    protected void run() {
        while (true) {
            try {
                // 等待事件并处理
                int readyChannels = selector.select();
                if (readyChannels > 0) {
                    processReadyEvents();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}</code></pre>
                </div>
                <p class="mt-4 text-gray-600">在 <code>NioEventLoop</code> 中，<code>Selector</code> 监听多个通道的事件，如连接、读取、写入等。当一个通道有 I/O 事件时，<code>Selector</code> 会通知 <code>NioEventLoop</code>，然后处理相应的事件。</p>
            </div>
        </section>

        <!-- Section 3 -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <span class="text-5xl font-bold text-purple-600 opacity-20 mr-4">03</span>
                <h2 class="text-3xl font-bold">非阻塞 `Channel` 的工作原理</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="prose max-w-none">
                    <p><code>Channel</code> 是 Netty 中的核心组件，表示与网络的连接。Netty 使用 Java NIO 提供的 <code>SelectableChannel</code>，并通过 <code>select()</code> 和 <code>selectNow()</code> 方法来实现非阻塞操作。</p>
                    
                    <div class="code-block p-6 mt-6 overflow-x-auto" data-lang="Java">
                        <pre><code class="language-java">public class NioSocketChannel extends AbstractNioChannel {
    private final SocketChannel socketChannel;

    public NioSocketChannel() throws IOException {
        this.socketChannel = SocketChannel.open();
        this.socketChannel.configureBlocking(false); // 设置为非阻塞模式
    }

    @Override
    public void doRead() {
        try {
            int bytesRead = socketChannel.read(buffer);
            if (bytesRead > 0) {
                // 处理读取的数据
            } else if (bytesRead == 0) {
                // 数据没有准备好
            } else {
                // 连接关闭
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}</code></pre>
                    </div>
                </div>
                
                <div>
                    <div class="bg-white p-6 rounded-xl shadow-md">
                        <h3 class="font-bold text-xl mb-4">非阻塞模式关键点</h3>
                        <ul class="space-y-3">
                            <li class="flex items-start">
                                <span class="bg-purple-100 text-purple-600 rounded-full p-2 mr-3">
                                    <i class="fas fa-check"></i>
                                </span>
                                <span><code>configureBlocking(false)</code> 设置非阻塞模式</span>
                            </li>
                            <li class="flex items-start">
                                <span class="bg-purple-100 text-purple-600 rounded-full p-2 mr-3">
                                    <i class="fas fa-check"></i>
                                </span>
                                <span>读取操作立即返回，不会阻塞线程</span>
                            </li>
                            <li class="flex items-start">
                                <span class="bg-purple-100 text-purple-600 rounded-full p-2 mr-3">
                                    <i class="fas fa-check"></i>
                                </span>
                                <span>通过返回值判断操作状态</span>
                            </li>
                            <li class="flex items-start">
                                <span class="bg-purple-100 text-purple-600 rounded-full p-2 mr-3">
                                    <i class="fas fa-check"></i>
                                </span>
                                <span>事件驱动处理I/O操作</span>
                            </li>
                        </ul>
                    </div>
                    
                    <div class="mt-6 bg-blue-50 p-6 rounded-xl border border-blue-100">
                        <h3 class="font-bold text-xl mb-2 text-blue-800">技术要点</h3>
                        <p class="text-blue-700">在非阻塞模式下，当执行读取操作时，如果没有数据可读，线程不会被阻塞，而是继续执行其他任务。这种方式显著提高了系统的并发处理能力。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 4 -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <span class="text-5xl font-bold text-purple-600 opacity-20 mr-4">04</span>
                <h2 class="text-3xl font-bold">事件循环（EventLoop）的管理</h2>
            </div>
            
            <div class="prose max-w-none">
                <p>Netty 使用事件循环来处理 I/O 事件，确保非阻塞操作能够异步地进行。在事件循环中，多个 I/O 操作被调度和处理，每个线程都负责管理一组连接。</p>
                
                <div class="code-block p-6 mt-6 overflow-x-auto" data-lang="Java">
                    <pre><code class="language-java">public interface EventLoopGroup extends Closeable {
    EventLoop next();
}</code></pre>
                </div>
                
                <p class="mt-4"><code>EventLoopGroup</code> 是管理多个事件循环线程的容器。每个事件循环线程会处理一组通道的 I/O 事件。<code>EventLoop</code> 会不断地从 <code>Selector</code> 获取 I/O 事件，并将其分发到相应的处理器。</p>
                
                <div id="diagram" class="mt-10 bg-white p-6 rounded-xl shadow-md">
                    <h3 class="text-2xl font-bold mb-6 text-center">Netty事件循环架构图</h3>
                    <div class="mermaid">
                        graph TB
                            A[EventLoopGroup] --> B[EventLoop 1]
                            A --> C[EventLoop 2]
                            A --> D[EventLoop 3]
                            B --> E[Selector]
                            C --> F[Selector]
                            D --> G[Selector]
                            E --> H[Channel 1]
                            E --> I[Channel 2]
                            F --> J[Channel 3]
                            G --> K[Channel 4]
                            style A fill:#6e8efb,color:white
                            style B fill:#a777e3,color:white
                            style C fill:#a777e3,color:white
                            style D fill:#a777e3,color:white
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 5 -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <span class="text-5xl font-bold text-purple-600 opacity-20 mr-4">05</span>
                <h2 class="text-3xl font-bold">非阻塞 I/O 操作流程</h2>
            </div>
            
            <div class="prose max-w-none">
                <ol class="list-decimal pl-6 space-y-4">
                    <li><strong>连接事件</strong>：当客户端或服务端通过 <code>Channel</code> 进行连接时，<code>Channel</code> 会注册到 <code>Selector</code> 上。</li>
                    <li><strong>选择器等待</strong>：事件循环（<code>EventLoop</code>）线程会调用 <code>selector.select()</code> 或 <code>selector.selectNow()</code> 等待 I/O 事件的发生。</li>
                    <li><strong>事件分发</strong>：当 <code>Selector</code> 检测到某个通道有事件发生（如连接、可读或可写），它会返回，并通知事件循环进行处理。</li>
                    <li><strong>处理事件</strong>：事件循环线程处理事件，比如读取数据或写入数据。如果当前事件无法立即完成，事件循环会返回，继续处理其他任务。</li>
                </ol>
                
                <div class="code-block p-6 mt-6 overflow-x-auto" data-lang="Java">
                    <pre><code class="language-java">public abstract class AbstractNioChannel extends AbstractChannel {
    protected final Selector selector;

    @Override
    protected void doRegister() {
        // 注册到选择器
        socketChannel.register(selector, SelectionKey.OP_READ);
    }

    @Override
    public void doWrite(ChannelOutboundBuffer in) {
        // 写入数据
        socketChannel.write(in);
    }
}</code></pre>
                </div>
            </div>
        </section>

        <!-- Section 6 -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <span class="text-5xl font-bold text-purple-600 opacity-20 mr-4">06</span>
                <h2 class="text-3xl font-bold">非阻塞 I/O 与 NIO 的关系</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="prose max-w-none">
                    <p>Netty 的非阻塞 I/O 基于 Java NIO（New I/O）实现，Java NIO 提供了 <code>Channel</code> 和 <code>Selector</code> 两个核心组件：</p>
                    
                    <ul class="space-y-2 mt-4">
                        <li><strong>Channel</strong>：用于读取或写入数据的接口，支持异步操作。</li>
                        <li><strong>Selector</strong>：用于检查多个通道是否有 I/O 事件发生的多路复用器。</li>
                    </ul>
                    
                    <div class="code-block p-6 mt-6 overflow-x-auto" data-lang="Java">
                        <pre><code class="language-java">public int select() throws IOException {
    // 选择器等待就绪事件
    int numSelected = selector.select();
    return numSelected;
}</code></pre>
                    </div>
                </div>
                
                <div class="bg-gray-100 rounded-xl p-6">
                    <h3 class="font-bold text-xl mb-4">NIO组件关系</h3>
                    <div class="mermaid">
                        graph LR
                            NIO --> Channel
                            NIO --> Selector
                            NIO --> Buffer
                            Channel --> SocketChannel
                            Channel --> ServerSocketChannel
                            Selector --> SelectionKey
                            style NIO fill:#6e8efb,color:white
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 7 -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <span class="text-5xl font-bold text-purple-600 opacity-20 mr-4">07</span>
                <h2 class="text-3xl font-bold">非阻塞 I/O 的优势</h2>
            </div>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-gradient-to-br from-purple-50 to-blue-50 p-6 rounded-xl">
                    <div class="text-purple-600 mb-4">
                        <i class="fas fa-rocket text-4xl"></i>
                    </div>
                    <h3 class="font-bold text-xl mb-2">高并发</h3>
                    <p>一个线程可以处理多个通道的 I/O 事件，避免了为每个连接创建单独线程的开销。</p>
                </div>
                
                <div class="bg-gradient-to-br from-blue-50 to-indigo-50 p-6 rounded-xl">
                    <div class="text-blue-600 mb-4">
                        <i class="fas fa-tachometer-alt text-4xl"></i>
                    </div>
                    <h3 class="font-bold text-xl mb-2">降低阻塞</h3>
                    <p>在等待 I/O 完成时，线程可以继续执行其他任务，提高 CPU 利用率。</p>
                </div>
                
                <div class="bg-gradient-to-br from-indigo-50 to-purple-50 p-6 rounded-xl">
                    <div class="text-indigo-600 mb-4">
                        <i class="fas fa-bolt text-4xl"></i>
                    </div>
                    <h3 class="font-bold text-xl mb-2">响应时间短</h3>
                    <p>通过事件驱动的方式，Netty 可以在事件发生时快速响应，处理 I/O 事件。</p>
                </div>
            </div>
            
            <div class="mt-10 bg-white p-6 rounded-xl shadow-md">
                <div class="mermaid">
                    pie title 非阻塞I/O优势比较
                        "高并发" : 40
                        "低资源占用" : 30
                        "快速响应" : 30
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12">
        <div class="container mx-auto max-w-5xl px-4 md:px-0">
            <div class="flex flex-col items-center text-center">
                <h3 class="text-2xl font-bold text-white mb-4">技术小馆</h3>
                <p class="mb-6 max-w-md">探索技术前沿，分享专业知识</p>
                <a href="http://www.yuque.com/jtostring" class="text-purple-400 hover:text-white transition duration-300">
                    <i class="fas fa-globe mr-2"></i> http://www.yuque.com/jtostring
                </a>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>
```